package DAO;

import java.io.File;
import java.util.ArrayList;

import photos.core.commons.Contants;



import DTO.AlbumModel;
import DTO.PhotoModel;
import Help.Manager.Album.BitmapHelpers;
import Help.Manager.Album.ExternalStorageHelpers;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

/*
 * Class PhotosData chá»©a thÃ´ng tin hÃ¬nh áº£nh cÃ³ trong SdCard.
 */
@SuppressLint("SdCardPath")
public class PhotosData {
	/*
	 * Danh sÃ¡ch hÃ¬nh áº£nh.
	 */
	private ArrayList<PhotoModel> photos;
	
	/*
	 * XÃ¡c Ä‘á»‹nh Ä‘Ã£ load dá»¯ liá»‡u hay chÆ°a.
	 */
	private boolean isLoaded;
	
	public PhotosData() {
		this.photos = new ArrayList<PhotoModel>();
	}
	
	public boolean isLoaded() {
		return isLoaded;
	}

	@SuppressWarnings("unused")
	private void load(Context context) {
		this.photos.clear();
		
		this.isLoaded = false;
		
		String[] projection = new String[] { 
				MediaStore.Images.Media._ID,
				MediaStore.Images.Media.DATA,
				MediaStore.Images.Media.DISPLAY_NAME,
				MediaStore.Images.Media.DATE_ADDED};
		
		Cursor cur = context.getContentResolver().query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, 
				null,
				null,
				null);
		
		while(cur.moveToNext()) {
			PhotoModel photo = new PhotoModel();
			int columnIndex = 0;
			
			columnIndex = cur.getColumnIndex(
					MediaStore.Images.Media.DISPLAY_NAME);
			if(columnIndex != -1) {
				photo.setTitle(cur.getString(columnIndex));
			}
			
			columnIndex = cur.getColumnIndex(
					MediaStore.Images.Media.DATA);
			if(columnIndex != -1) {
				photo.setPath(cur.getString(columnIndex));
			}
			
			columnIndex = cur.getColumnIndex(
					MediaStore.Images.Media._ID);
			if(columnIndex != -1) {
				photo.setId(cur.getLong(columnIndex));
			}
			
			columnIndex = cur.getColumnIndex(
					MediaStore.Images.Media.DATE_ADDED);
			if(columnIndex != -1) {
				photo.setDate(cur.getLong(columnIndex));
			}
			
			this.photos.add(photo);
		}
		
		this.isLoaded = true;
	}
	
	public ArrayList<PhotoModel> getPhotos() {
		return this.photos;
	}
	
	/*
	 * Láº¥y ArrayList chá»©a Ä‘Æ°á»�ng dáº«n cá»§a táº¥t cáº£ hÃ¬nh áº£nh Ä‘Ã£ load.
	 */
	public ArrayList<String> getImagesPath() {
		ArrayList<String> ret = new ArrayList<String>();
		for(PhotoModel photo : this.photos) {
			ret.add(photo.getPath());
		}
		
		return ret;
	}
	
	/*
	 * Load hÃ¬nh áº£nh tá»« SdCard.
	 */
	public void loadPhotosFromExternalStorage() throws Exception {
		if(!ExternalStorageHelpers.externalStorageAvailable()){
			throw new Exception();
		}
		
		this.photos.clear();
		this.isLoaded = false;
		//this.load(android.os.Environment.getExternalStorageDirectory());
		this.load(new File("/sdcard/EasyNotes"));
		this.isLoaded = true;
	}
	
	/*
	 * Load hÃ¬nh áº£nh trong thÆ° má»¥c chá»‰ Ä‘á»‹nh.
	 * PhÆ°Æ¡ng thá»©c Ä‘á»‡ qui.
	 */
	private void load(File currentDir) {
		if(!currentDir.isDirectory())
			return;
		
		// KhÃ´ng hiá»ƒn thá»‹ hÃ¬nh áº£nh thumbnail táº¡o bá»Ÿi há»‡ thá»‘ng.
		if(currentDir.getName().equalsIgnoreCase(Contants.ThumbnailFolder))
			return;
		
		File[] files = currentDir.listFiles();

		for(File file : files) {
			if(file.isFile()) { // File.
				String path = file.getAbsolutePath();
				long lastModified = file.lastModified();
				
				if(!BitmapHelpers.isImage(path))
					continue;
				
				PhotoModel photo = new PhotoModel();
				
				photo.setPath(path);
				photo.setDate(lastModified);
				
				this.photos.add(photo);
			}
			else { // Directory.
				this.load(file);
			}
		}
	}
	
	public void removePhoto(PhotoModel photo, AlbumModel album, AlbumsController albumsCtrl) {
		albumsCtrl.removePhoto(photo, album);
		//this.photos.remove(photo);
		this.deletePhoto(photo);
	}
	
	public void removePhoto(int position, AlbumModel album, AlbumsController albumsCtrl) {
		PhotoModel photo = album.getPhoto(position);
		this.removePhoto(photo, album, albumsCtrl);
	}
	
	private void deletePhoto(PhotoModel photo) {
		this.photos.remove(photo);
		ExternalStorageHelpers.deleteFile(photo.getPath());
	}
	
	public void removeAlbum(AlbumsController albumsCtrl, AlbumModel album) {
		for(PhotoModel photo : album.getPhotos()) {
			//this.photos.remove(photo);
			this.deletePhoto(photo);
		}
		
		albumsCtrl.removeAlbums(album);
	}
}
